package com.site.blog.config.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @Author guo peng
 * 描述:  日志信息aop
 * 创建时间:2020/8/1 9:43
 * 修改时间:2020/8/1 9:43
 */
@Slf4j
@Aspect
@Component
@ConditionalOnProperty(prefix = "logAop",name = "switch",havingValue = "true")
public class LogAspect {

    /**
     * 设置controller层操作日志切入点
     */
    @Pointcut("execution(public * com.site.blog.controller..*.*(..))")
    public void controllerLog(){}//签名，可以理解成这个切入点的一个名称

    /**
     * 设置service层操作日志切入点
     */
    @Pointcut("execution(public * com.site.blog.service.impl..*.*(..))")
    public void serviceLog(){}

    /**
     * 设置mapper层操作日志切入点
     */
    @Pointcut("execution(public * com.site.blog.dao..*.*(..))")
    public void mapperLog(){}

    /**
     * controller层 环绕通知 记录日志
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("controllerLog()")
    public Object monitor1(ProceedingJoinPoint joinPoint) throws Throwable {
        String targetClassName = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
        log.info("***********************request start***************************");
        log.info("------------controller层开始执行: " + targetClassName+"->"+methodName+"()方法");
        log.info("------------方法全路径: " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        log.info("------------请求地址: " + request.getRequestURL().toString());
        log.info("------------请求方式: " + request.getMethod());
        log.info("------------请求IP: " + request.getRemoteAddr());
        log.info("------------请求参数: " + Arrays.toString(joinPoint.getArgs()));
        Object result = null;
        long delay = 0;
        try {
            long begin = System.currentTimeMillis();
            result = joinPoint.proceed();
            delay = System.currentTimeMillis() - begin;
            String resultData = result==null? "null": result.toString();
            log.info("------------执行结果: " + resultData);
            log.info("------------"+targetClassName+"的controller->"+methodName+"()方法执行完毕,用时:" + delay + "ms");
        } catch (Exception e) {
            log.info("------------controller执行异常: " + targetClassName+"->"+methodName+"()方法");
            throw e;
        } finally {
            log.info("***********************request end***************************");
        }

        return result;
    }

    /**
     * service层 环绕通知 记录日志
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("serviceLog()")
    public Object monitor(ProceedingJoinPoint joinPoint) throws Throwable {
        String targetClassName = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        Object result = null;
        long delay = 0;
        try {
            log.info("--------service层开始执行: "+targetClassName+"->"+methodName+"()方法");
            log.info("--------方法全路径: " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
            log.info("--------参数列表: " + Arrays.toString(joinPoint.getArgs()));
            long begin = System.currentTimeMillis();
            result = joinPoint.proceed();
            String resultData = result==null? "null": result.toString();
            log.info("--------service层执行结果: " + resultData);
            delay = System.currentTimeMillis() - begin;
        } catch (Exception e) {
            log.info("--------service执行异常: "+targetClassName+"->"+methodName+"()方法");
            throw e;
        } finally {

                log.info("--------"+targetClassName+"->"+methodName+"()方法执行完毕,用时:" + delay + "ms");
        }

        return result;
    }

    /**
     * mapper层 环绕通知 记录日志
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("mapperLog()")
    public Object monitor3(ProceedingJoinPoint joinPoint) throws Throwable {
        String targetClassName = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        Object result = null;
        long delay = 0;
        try {
            log.info("----mapper层开始执行: "+targetClassName+"->"+methodName+"()方法");
            log.info("----方法全路径: " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
            log.info("----参数列表: " + Arrays.toString(joinPoint.getArgs()));
            long begin = System.currentTimeMillis();
            result = joinPoint.proceed();
            delay = System.currentTimeMillis() - begin;
            String resultData = result==null? "null": result.toString();
            log.info("----mapper层执行结果: " + resultData);
        } catch (Exception e) {
            log.info("----mapper执行异常: "+targetClassName+"->"+methodName+"()方法");
            throw e;
        } finally {
            log.info("----"+targetClassName+"->"+methodName+"()方法执行完毕,用时:" + delay + "ms");
        }

        return result;
    }

}
